package cn.tx.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import cn.tx.model.Menu;
import cn.tx.model.Role;
import cn.tx.query.MenuQuery;
import cn.tx.service.MenuService;
import cn.tx.service.RoleService;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class MenuAction extends BaseAction implements ModelDriven<Menu>{
	
	private Menu menu = new Menu();
	
	private MenuService menuService;
	
	private Integer roleId;
	
	
	private RoleService roleService;
	
	
	
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * 查询对象
	 */
	private MenuQuery erpQuery = new MenuQuery();
	
	
	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	

	public MenuQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(MenuQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	/**
	 * 查询员工的列表
	 * @return
	 */
	public String menu_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = menuService.selectObjectByConditionPage(erpQuery, exclude);
		context.put("page", page);
		
		return SUCCESS;
	}
	public String menu_poplist(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		//Page page = menuService.selectObjectByConditionPage(erpQuery, exclude);
		
		Menu rootMenu = menuService.getObjectById(1);
		JSONArray ja = new JSONArray();
		//查询到角色的对象
		Role role = roleService.getObjectById(roleId);
		//获得当前角色的菜单集合
		Set<Menu> menus = role.getMenus();
		createTree(ja, rootMenu, menus);
		//json的数据可以通过页面的js直接获得。
		context.put("zNodes", ja);
		return SUCCESS;
	}
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String menu_input(){
		
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String menu_preupdate(){
		menu = menuService.getObjectById(menu.getMenuId());
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 */
	public String menu_save(){
		menuService.save(menu);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String menu_update(){
		menuService.update(menu);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String menu_delete(){
		menu = menuService.getObjectById(menu.getMenuId());
		menuService.delete(menu);
		return TOLIST;
	}

	@Override
	public Menu getModel() {
		return menu;
	}
	
	public void createTree(JSONArray ja, Menu menu, Set<Menu> menus){
		Map<String, Object> map = new HashMap<String, Object>();
		if(menu != null){
			map.put("id", menu.getMenuId());
			if(menu.getParent() != null){
				map.put("pId", menu.getParent().getMenuId());
			}else{
				map.put("pId",0);
			}
			
			map.put("name", menu.getName());
			if(menu.getMenuId().intValue() == 1){
				map.put("open", true);
			}
			//判断是否当前角色的拥有的权限菜单
			for(Menu mymenu : menus){
				if(mymenu.getMenuId().intValue() == menu.getMenuId().intValue()){
					map.put("checked", true);
					map.put("open", true);
					break;
				}
			}
			ja.add(map);
			Set<Menu> childs = menu.getChilds();
			if(childs != null && childs.size() > 0){
				for(Menu m : childs){
					createTree(ja,m, menus);
				}
			}
		}
		
	
	}
	
	

}
